package init

import (
	"encoding/json"
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
	"user_srv/basic/config"
)

func InNacos() {
	clientConfig := constant.ClientConfig{
		NamespaceId:         "0f5eb9b8-480c-4ded-acea-d504c0e388ea", //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "debug",
	}
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      "14.103.175.162",
			ContextPath: "/nacos",
			Port:        8848,
			Scheme:      "http",
		},
	}
	configClient, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &clientConfig,
			ServerConfigs: serverConfigs,
		},
	)
	if err != nil {
		panic("nacos init failed")
	}
	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: "Laoyuegou_Social",
		Group:  "DEFAULT_GROUP"})

	_ = json.Unmarshal([]byte(content), &config.AppConf)

	_ = configClient.ListenConfig(vo.ConfigParam{
		DataId: "Laoyuegou_Social",
		Group:  "DEFAULT_GROUP",
		OnChange: func(namespace, group, dataId, data string) {
			fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
		},
	})
}
